package com.kkb.controller;
import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Game;
import com.kkb.service.GameService;
import com.kkb.vo.GameQueryVo;
import com.kkb.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/game")
@ResponseBody
public class GameController {
    @Autowired
    private GameService gameService;
    /**
     * 根据多条件去查询game列表中的数据
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultVo<Game> queryByPage(Integer pageNum, Integer pageSize, GameQueryVo vo){
        if (pageNum==null || pageNum<0){
            pageNum=1;
        }
        if (pageSize==null || pageSize<0){
            pageSize=5;
        }
        PageInfo<Game> gamePageInfo = gameService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo<Game>(gamePageInfo);
    }

    /**
     * 添加一个比赛信息
     * @param game
     * @param req
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVo<Game> addGame(Game game, HttpServletRequest req){
        String hTCN = req.getParameter("homeTeamChineseName");
        String vTCN = req.getParameter("visitingTeamChineseName");
        if (gameService.queryChineseName(hTCN)!=null && gameService.queryChineseName(vTCN)!=null){
            Integer hTCNId = gameService.queryChineseName(hTCN);
            Integer vTCNId = gameService.queryChineseName(vTCN);
            game.setHomeTeamId(hTCNId); game.setVisitingTeamId(vTCNId);
            //进行添加操作
            int i = gameService.addGame(game);
            if (i!=1){
                return new ResultVo<>(500,"服务器内部出现错误，请稍后再试！");
            }
            return new ResultVo<>();

        }else{
            return new ResultVo<>(500,"请检查您输入球队的中文名称");
        }
    }
    /**
     * 通过gameId去查询一条数据
     */
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVo<Game> queryById(@PathVariable("id") Integer gameId){
       return new ResultVo<Game>(gameService.queryById(gameId));
    }
    /**
     * 更新一条数据
     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Game> updateGame(@PathVariable("id")Integer gameId,Game game){
        game.setGameId(gameId); //因为前端页面（disabled）没有传递过来gameId,所以需要我们手动进行添加
        int i = gameService.updateGame(game);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器出现错误，请稍后再试！");
    }
    /**
     * 根据id去删除一条比赛记录
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVo<Game> deleteGame(@PathVariable("id")Integer gameId){
        int i = gameService.deleteGame(gameId);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器内部出错，请稍后再试！");
    }
}
